import common from '../common'
import {CHANNEL} from 'utils/config'
import callWxLogin from './utils'

export default {
  navigateTo: common.navigateTo,
  payOrderSDK: common.payOrderSDK,
  callPhone: common.callPhone,
  gotoHomePage: common.gotoHomePage,
  getOffset: common.getOffset,
  getAllOffset: common.getAllOffset,
  getSelectCity: common.getSelectCity,
  selectCity: common.selectCity,
  getSafeAreaHeight: common.getSafeAreaHeight,
  // 打开地图
  openMapVC(params) {
    let wxsdk = window.wx
    if (wxsdk) {
      // console.log('wxsdk', wxsdk)
      let {gLat, gLng, chainName, address} = params || {}
      if (!gLat || !gLng || !chainName || !address) {
        console.error('[sdk openMapVC]::: latitude and longitude should not be empty')
      }
      // 获取腾讯坐标
      wxsdk.openLocation({
        latitude: Number(gLat),
        longitude: Number(gLng),
        name: chainName, // 位置名
        address: address, // 地址详情说明
        scale: 12 // 这个缩放比例最合适
      })
    }
  },

  // 返回事件监听
  clickLeftBT(callback, vm) {
    let state = {
      title: 'title',
      url: '#'
    }
    // console.log('state--->', window.history.state)
    if (window.history.state.title != 'title' && window.history.state.url != '#') {
      window.history.pushState(state, 'title', '#')
    }
    window.addEventListener('popstate', callback, {once: true})
    vm.$once('hook:beforeDestroy', () => {
      window.removeEventListener('popstate', callback, {once: true})
    })
    return {
      remove() {
        window.removeEventListener('popstate', callback, {once: true})
      }
    }
  },
  // 获取客户端信息
  getClientInfo() {
    // sellerId
    let sellerIdMap = {
      '90044': 619969, // 铂涛旅行公众号
      '167': 622058, // 锦江酒店公众号
      '349': 513421 // 尊享会员公众号
    }
    return new Promise((resolve, reject) => {
      resolve({sellerId: sellerIdMap[CHANNEL]})
    })
  },

  getSellerId() {
    return new Promise((resolve, reject) => {
      this.getClientInfo().then(data => {
        resolve(data.sellerId)
      }).catch(() => {
        resolve()
      })
    })
  },

  async getToken(notCallLogin = false, callbackUrl) {
    // 微信端
    let token = localStorage.getItem('AUTHORIZATION')
    if (!token && !notCallLogin) {
      this.login({callbackUrl})
    }

    if (token) {
      store.commit('SAVE_TOKEN', token)
      
      return Promise.resolve(token)
    } else {
      return Promise.reject()
    }
  },

  // TODO 未测试
  login(data) {
    let {callbackUrl, notCallLogin} = data || {}
    return new Promise((resolve) => {
      // 获取本地缓存token
			let token = storage.get('AUTHORIZATION')
			if (token) {
				store.commit('SAVE_TOKEN', loginResp.data)

				resolve(token)
				return
      }
      
      if (notCallLogin) {
        resolve({})
				return
      }

      // 微信登录会直接跳转，此处无需resolve，但是为了兼容app写法，此处返回Promise
      callWxLogin(callbackUrl)
      // 注意：不要resolve，否则会执行app渠道的then回调导致可能出现的bug
    })
  },
  /**
   * 获取状态栏高度（仅适用于webview包含状态栏的情形）
   */
  getStatusBarHeight() {
    return 0
  },

  // TODO 获取定位
  getLocation() {
    return new Promise((resolve, reject) => {
      // TODO 获取定位
      reject({})
    })
  },
  // TODO 获取定位
  gainLocation() {
    return new Promise((resolve, reject) => {
      reject('mobile 定位失败')
    })
  },
  // 复制
  setClipboardData(data) {
    return new Promise((success, fail) => {
      uni.setClipboardData({ data, success, fail });
    })
  },
  // 获取剪切板内容
  getClipboardData() {
    return new Promise((success, fail) => {
      uni.getClipboardData({ success, fail });
    });
  }
}
